/*
 * Copyright (c) zhg2yqq.com Corp.
 * All Rights Reserved.
 */
package com.zhg2yqq.wheels.mvc.protection;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.zhg2yqq.wheels.mvc.protection.PreventResubmit.PreventResubmits;

/**
 * @author zhg2yqq, 2023年3月22日
 * @version zhg2yqq v1.0
 */
@Aspect
public class PreventResubmitAspect {
    private IPreventResubmitService preventResubmitService;

    public PreventResubmitAspect(IPreventResubmitService preventResubmitService) {
        this.preventResubmitService = preventResubmitService;
    }

    @Around(value = "@annotation(preventResubmit)")
    public Object preventResubmit(ProceedingJoinPoint point, PreventResubmit preventResubmit) throws Throwable {
        preventResubmitService.preResubmit(preventResubmit, point);
        try {
            return point.proceed();
        } catch (Throwable e) {
            preventResubmitService.removeResubmit(preventResubmit, point);
            throw e;
        }
    }

    @Around(value = "@annotation(preventResubmits)")
    public Object preventResubmits(ProceedingJoinPoint point, PreventResubmits preventResubmits) throws Throwable {
        for (PreventResubmit preventResubmit : preventResubmits.value()) {
            preventResubmitService.preResubmit(preventResubmit, point);
        }
        try {
            return point.proceed();
        } catch (Throwable e) {
            for (PreventResubmit preventResubmit : preventResubmits.value()) {
                preventResubmitService.removeResubmit(preventResubmit, point);
            }
            throw e;
        }
    }
}
